//
//  SNReturnGoodsRepetitiousViewController.m
//  app
//
//  Created by 王少宁 on 15/5/21.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//  多次 申请售后

#import "SNReturnGoodsRepetitiousViewController.h"
#import "SNReturnGoodsRepetitiousHeaderView.h"
#import "SNReturnGoodsViewController.h"
#import "SNReturnGoodsDataManager.h"
#import "SNReturnGoodsRepetitiousCell.h"
#import "SNOrderFormDetailViewController.h"

@interface SNReturnGoodsRepetitiousViewController ()<UITableViewDataSource,UITableViewDelegate,SNReturnGoodsRepetitiousHeaderViewDeletage>
@property (nonatomic, retain) UITableView * myTableView;
@property (nonatomic, retain) SNReturnGoodsRepetitiousDataManager * manager;
@property (nonatomic, retain) SNReturnGoodsRepetitiousHttpResult * result;
@end

@implementation SNReturnGoodsRepetitiousViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"申请售后";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createBackBtn];
    
    self.result = [[SNReturnGoodsRepetitiousHttpResult alloc] initWithTarget:self action:@selector(loadData:)];
    self.manager = [SNReturnGoodsRepetitiousDataManager shareManager];
    [self createTableView];
    [self requestWithSn:self.sn productId:self.productId];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestWithSn:self.sn productId:self.productId];
}

- (void)createBackBtn
{
    UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 20, 100, 40);
    [backBtn setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ic_actionbar_home_up@2x" ofType:@"png"]] forState:UIControlStateNormal];
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -105, 0, 0);
    [backBtn addTarget:self action:@selector(popToOrderFormDetailVC) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * backBtnItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backBtnItem;
}

- (void)popToOrderFormDetailVC
{
    for (UIViewController *temp in self.navigationController.viewControllers) {
        if ([temp isKindOfClass:[SNOrderFormDetailViewController class]]) {
            [self.navigationController popToViewController:temp animated:YES];
        }
    }
}

- (void)requestWithSn:(NSString *)sn productId:(NSString *)productId
{
    [self.manager returngoodsLoadDataWithResult:self.result sn:sn productId:productId];
}

- (void)createTableView
{
    self.myTableView = CREATE_WITH_FRAME_OF(UITableView, [self.view.ufo fx:0 oy:self.navigationAndStatusBarHeight],
                                            [self.view.ubo fw:1.0 fh:1.0 oh:-self.navigationAndStatusBarHeight]);
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.nothingRefreshView = [[UIRefreshNotingRefreshView alloc] initWithView:self.myTableView viewController:self tryAgain:@selector(requestTryAgain)];
    [self.view addSubview:self.myTableView];
}

- (void)requestTryAgain
{
    [self.myTableView.nothingRefreshView show];
    [self requestWithSn:self.sn productId:self.productId];
}

- (void)loadData:(SNReturnGoodsRepetitiousHttpResult *)result
{
    if (result.success) {
        [self.myTableView.nothingRefreshView remove];
        [self tableViewCreateHeaderView];
        [self.myTableView reloadData];
    } else {
        
        [self.myTableView.nothingRefreshView activeStop];
    }
}

- (void)tableViewCreateHeaderView
{
    SNReturnGoodsRepetitiousHeaderView * headerView = [[SNReturnGoodsRepetitiousHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 194) sn:self.sn createTime:self.result.model.createTime receiver:self.result.model.receiver_name isReturns:self.result.model.isReturns ostatus:self.result.model.ostatus goodsModel:self.listModel];
    headerView.deletage = self;
    self.myTableView.tableHeaderView = headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.result.model.askAndRespondArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    SNReturnGoodsRepetitiousCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[SNReturnGoodsRepetitiousCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell cellWithModel:self.result.model.askAndRespondArray[indexPath.row] row:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SNReturnGoodsAskAndRespondModel * model = self.result.model.askAndRespondArray[indexPath.row];
    return model.cellHeight;
}

#pragma mark - 点击了 继续售后 按钮
- (void)nextServeBtnInHeaderViewClick
{
    SNReturnGoodsViewController * returnGoodsVC = [[SNReturnGoodsViewController alloc] init];
    returnGoodsVC.sn = self.sn;
    returnGoodsVC.receiverName = self.result.model.receiver_name;
    returnGoodsVC.isReturns = self.result.model.isReturns;
    returnGoodsVC.ostatus = self.result.model.ostatus;
    returnGoodsVC.timeStr = self.result.model.createTime;
    returnGoodsVC.receiverPhoneNum = self.receiverPhoneNum;
    returnGoodsVC.listModel = self.listModel;
    returnGoodsVC.popToVCNameStr = @"SNReturnGoodsRepetitiousViewController";
    [self.navigationController pushViewController:returnGoodsVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
